local event_priority_queue  = {}

local HU = 4
local GANG = 3
local PENG = 2
local CHI = 1
function event_priority_queue:ctor()
	self.queue = {}
end

function event_priority_queue:insert(event)
	table.insert(self.queue,event)
end

function event_priority_queue:remove(user)
	--local user = event.user
	for i = #self.queue,1,-1 do
		if self.queue[i].user == user then
			table.remove(self.queue,i)
		end
	end
end

function event_priority_queue:sort()
	function queue_sort(event1,event2)
		if event1.type == HU and event2.type == HU then
			return event1.priority > event2.priority
		else
			return event1.type > event2.type
		end
	end
	table.sort(self.queue,queue_sort)
end

function event_priority_queue:get_queue()
	return self.queue
end

function event_priority_queue:confirm(user,tp,message,is_gang)
	for i=#self.queue,1 ,-1 do
		--local v = self.queue[i]
		if user == self.queue[i].user and tp == self.queue[i].type then
			self.queue[i].message = self.queue[i].message or message
			self.queue[i].confirm = true
			self.queue[i].is_gang = is_gang
		end
		if user == self.queue[i].user and tp ~= self.queue[i].type then
			table.remove(self.queue,i)
		end
	end
end

function event_priority_queue:pop_first_event()
	return table.remove(self.queue,1)
end

function event_priority_queue:get_first_event()
	return self.queue[1]
end

function event_priority_queue:get_first_confirmed_event()
	local eve = self:get_first_event()
	if eve.type == HU then
		return eve
	end
	for i,v in ipairs(self.queue) do
		if v:is_confirm() then
			return v
		end
	end
end

function event_priority_queue:get_all_hu()
	local events = {}
	local users = {}
	for k,v in pairs(self.queue) do
		if v.type == HU then
			table.insert(events,v.message)
			table.insert(users,v.user)
		end
	end
	return events,users
end

function event_priority_queue:is_confirm_hu()
	for k,v in pairs(self.queue) do
		if v.type == HU then
			if not v:is_confirm() then
				return false
			end
		end
	end
	return true
end

function event_priority_queue:has(user,tp)
	for i,v in ipairs(self.queue) do
		if v.user == user and v.type == tp then
			return true
		end
	end
	return false
end

function event_priority_queue:size()
	return #self.queue
end

local M = {}
function M.new()
	return setmetatable({},{__index = event_priority_queue})
end
return M